package com.spa.infrastructure.service.douyin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.spa.infrastructure.enums.dict.base.SourceTypeEnum;
import com.spa.infrastructure.service.douyin.enterprise.DouYinEnterpriseAppConstant;
import com.spa.infrastructure.service.douyin.param.CancelParam;
import com.spa.infrastructure.service.douyin.param.VerifyParam;
import com.spa.infrastructure.service.douyin.result.CancelResult;
import com.spa.infrastructure.service.douyin.result.PrepareResult;
import com.spa.infrastructure.service.douyin.result.ShopPoiQueryResult;
import com.spa.infrastructure.service.douyin.result.VerifyResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DouYinGoodLifeService extends DouYinService{

    @Value("${douYin.goodLife.accountId}")
    private String accountId;

    @Value("${douYin.goodLife.appId}")
    private String appId;

    @Value("${douYin.goodLife.secret}")
    private String secret;

    /**
     * 查询门店信息 基本不需要 这个应该是提前匹配好的
     * @return
     * @throws JsonProcessingException
     */
    public ShopPoiQueryResult queryShop() throws JsonProcessingException {
        String token = getAccessToken();
        String url = DouYinEnterpriseAppConstant.SHOP_POI_QUERY + "?account_id=" + accountId + "&page=1&size=10";
        ShopPoiQueryResult shopPoiQueryResult = mapper.readValue(sendGet4DouYinV2(url, token), ShopPoiQueryResult.class);
        log.info("ShopPoiQueryResult {}", shopPoiQueryResult);
        return shopPoiQueryResult;
    }

    /**
     * 抖音团购券码的核销, 需要先调用本接口, 查询订单的券列表，选择要验的券，再调用验券接口，核销券码。
     */
    public PrepareResult prepareByEncryptedData(String encryptedData) throws JsonProcessingException {
        String token = getAccessToken();
        String url = DouYinEnterpriseAppConstant.FULFILMENT_CERTIFICATE_PREPARE + "?encrypted_data=" + encryptedData;
        String response = sendGet4DouYinV2(url, token);
        PrepareResult prepareResult = mapper.readValue(response, PrepareResult.class);
        log.info("PrepareResult {}", prepareResult);
        return prepareResult;
    }

    public PrepareResult prepareByCode(String code) throws JsonProcessingException {
        String token = getAccessToken();
        String url = DouYinEnterpriseAppConstant.FULFILMENT_CERTIFICATE_PREPARE + "?code=" + code;
        String response = sendGet4DouYinV2(url, token);
        PrepareResult prepareResult = mapper.readValue(response, PrepareResult.class);
        log.info("PrepareResult {}", prepareResult);
        return prepareResult;
    }

    /**
     * 用于核销券码，同时适用于抖音团购券码与三方券码，抖音券码的核销需要先调用准备接口，再调用本接口，三方券码的核销直接调用本接口即可。
     * @param prepareResult
     * @return
     * @throws JsonProcessingException
     */
    public VerifyResult verify(PrepareResult prepareResult, String poiId) throws JsonProcessingException {
        String token = getAccessToken();
        String url = DouYinEnterpriseAppConstant.FULFILMENT_CERTIFICATE_VERIFY;
        List<String> codes = prepareResult.getData().getCertificates().stream().map(PrepareResult.Certificate::getEncrypted_code).toList();
        VerifyParam verifyParam = VerifyParam.builder()
                .verify_token(prepareResult.getData().getVerify_token())
                .poi_id(poiId)
                .encrypted_codes(codes)
                .build();
        String response = sendPost4DouYinV3(url, mapper.writeValueAsString(verifyParam), token);
        VerifyResult verifyResult = mapper.readValue(response, VerifyResult.class);
        log.info("VerifyResult {}", verifyResult);
        return verifyResult;
    }

    /**
     * 用于撤销核销状态，误核销之后的撤销操作，有时间限制，验券超过一个小时就不可再撤销。
     * @return
     * @throws JsonProcessingException
     */
    public CancelResult cancel(String verifyId, String certificateId) throws JsonProcessingException {
        String token = getAccessToken();
        String url = DouYinEnterpriseAppConstant.FULFILMENT_CERTIFICATE_CANCEL;
        CancelParam cancelParam = CancelParam.builder()
                .verify_id(verifyId)
                .certificate_id(certificateId)
                .build();
        String response = sendPost4DouYinV3(url, mapper.writeValueAsString(cancelParam), token);
        CancelResult cancelResult = mapper.readValue(response, CancelResult.class);
        log.info("CancelResult {}", cancelResult);
        return cancelResult;
    }

    /**
     * 在核销的异常场景情况下，对抖音码团购、次卡券进行状态查询。
     * @param encryptedCode
     * @return
     * @throws JsonProcessingException
     */
    public String get(String encryptedCode) throws JsonProcessingException {
        String token = getAccessToken();
        String url = DouYinEnterpriseAppConstant.FULFILMENT_CERTIFICATE_GET + "?encrypted_code=" + encryptedCode;
        String response = sendGet4DouYinV2(url, token);
//        PrepareResult prepareResult = mapper.readValue(response, PrepareResult.class);
        log.info("PrepareResult {}", response);
        return response;
    }

    private String getAccessToken() {
        return getClientToken(appId, secret, SourceTypeEnum.DouYinEnterprise);
    }

}
